<template>
  <main-box>
    <PlusSearch
      v-model="queryform"
      :columns="formConfig"
      :show-number="2"
      label-width="80"
      label-position="right"
      class="mb-xs"
      @search="initData(true)"
      @reset="initData(true)"
    >
      <template #plus-field-warehouseId>
        <chooseWarehouse v-model="queryform.warehouseId" />
      </template>
    </PlusSearch>
    <div class="mb-2">
      <btnExport
        :queryform="queryform"
        :exportApi="exportApi"
        downloadName="设施货架统计"
      />
    </div>
    <PureTable
      ref="tableRef"
      border
      adaptive
      :loading="loading"
      :data="tableData"
      :columns="columns"
      :pagination="pagination"
      @page-size-change="initData()"
      @page-current-change="initData(false)"
    />
  </main-box>
</template>

<script setup lang="tsx">
import { onMounted, ref } from "vue";
import { useTableConfig } from "@/utils/tableConfig";
import { type PlusColumn, PlusSearch } from "plus-pro-components";
import btnExport from "@/components/btnExport/index.vue";

import { exportApi, listApi } from "@/api/statistics/facility";
import chooseWarehouse from "@/components/choose-warehouse/index.vue";

/* 查询表单 */
const formConfig: PlusColumn[] = [
  {
    label: "查询",
    prop: "searchValue",
    fieldProps: { placeholder: "设施编号/IP地址/物理地址" }
  },
  {
    label: "工具室",
    prop: "warehouseId"
  }
];
const warehouseId = (window as any).datasource;
const queryform = ref({
  searchValue: "",
  warehouseId: warehouseId > 0 ? warehouseId : ""
});
/* 表格数据 */
const { pagination } = useTableConfig();
const tableData = ref([]);
const loading = ref(false);
const columns = [
  {
    label: "设施图片",
    prop: "picUrl",
    cellRenderer: ({ row }) => {
      const { picUrl } = row;
      if (!picUrl) return "";
      let list = [];
      if (picUrl) {
        list = picUrl.split(",");
      }
      return list.map((item, index) => {
        return (
          <el-image
            src={item}
            width={50}
            initial-index={index}
            preview-src-list={list}
            preview-teleported={true}
          />
        );
      });
    }
  },
  { label: "设施编号", prop: "code" },
  { label: "设施名称", prop: "name" },
  {
    label: "联网状态",
    prop: "model",
    cellRenderer: ({ row }) => {
      const txt = row.networkState == 1 ? "在线" : "下线";
      const col = row.networkState == 1 ? "primary" : "info";
      return <el-tag type={col}>{txt}</el-tag>;
    }
  },
  { label: "IP地址", prop: "ipAddr" },
  { label: "物理地址", prop: "macAddr" },
  {
    label: "运行状态",
    prop: "state",
    cellRenderer: ({ row }) => {
      const txt = row.networkState == 1 ? "正常" : "停用";
      const col = row.networkState == 1 ? "success" : "danger";
      return <el-tag type={col}>{txt}</el-tag>;
    }
  },
  { label: "添加时间", prop: "createTime" },
  { label: "工具总数", prop: "toolCount", sortable: true },
  { label: "在柜库存", prop: "inStockCount", sortable: true },
  { label: "累计借出", prop: "totalLendCount", sortable: true }
];

const initData = async (needInit = true) => {
  loading.value = true;
  if (needInit) {
    pagination.currentPage = 1;
  }
  await listApi({
    pageNum: pagination.currentPage,
    pageSize: pagination.pageSize,
    ...queryform.value
  }).then(res => {
    tableData.value = res.rows;
    pagination.total = Number(res.total);
  });
  loading.value = false;
};

initData();
</script>
